/* CsJni - .NET / Java Bridge
 * 
 * Copyright 2010 Robert E. Burke Licensed under the Apache License, Version 
 * 2.0 (the "License"); you may not use this file except in compliance with 
 * the License. You may obtain a copy of the License at 
 * http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable 
 * law or agreed to in writing, software distributed under the License is 
 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 
 * KIND, either express or implied. See the License for the specific language 
 * governing permissions and limitations under the License. 
*/

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Globalization;
using CsJni.PlatformAbstraction;
using System.Collections.ObjectModel;

namespace CsJni
{
    public enum JavaVMDiscoveryType
    {
        Path,
        Lookup
    }

    internal sealed class JavaVMPathFinder
    {
        private JavaVMPathFinder()
        {
        }

        public static string FindPath(JavaVMConfiguration configuration)
        {
            int strategyCount = configuration.DiscoveryStrategyCount;
            for (int i = 0; i < strategyCount; i++)
            {
                JavaVMDiscoveryStrategy strategy = configuration.GetDiscoveryStrategyAtIndex(i);
                JavaVMDiscoveryType discoveryType = strategy.DiscoveryType;
                string discoverySpec = strategy.Specification;
                string workingSpec = discoverySpec == null ? string.Empty : discoverySpec;
                ReadOnlyCollection<String> hints = strategy.Hints;
                workingSpec = workingSpec.Trim();
                string outputPath = null;
                if (discoveryType == JavaVMDiscoveryType.Path)
                {
                    outputPath = PlatformFactory.Platform.FindJvmLibraryWithPath(workingSpec);
                }
                else if (discoveryType == JavaVMDiscoveryType.Lookup)
                {
                    outputPath = PlatformFactory.Platform.LookupJvmLibrary(workingSpec);
                }
                if (outputPath != null)
                {
                    return outputPath;
                }
            }
            throw new JniException("Failed to find JVM Path.");
        }
    }
}
